package com.seawaterbt.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seawaterbt.ssm.config.DataSource;
import com.seawaterbt.ssm.config.DataSourceEnum;
import com.seawaterbt.ssm.entity.BasCommunity;
import com.seawaterbt.ssm.entity.BasCrew;
import com.seawaterbt.ssm.entity.CzBasCrew;
import com.seawaterbt.ssm.entity.ShengBasCrew;
import com.seawaterbt.ssm.mapper.ShengBasCrewMapper;
import com.seawaterbt.ssm.service.CzBasCrewService;
import com.seawaterbt.ssm.service.ShengBasCrewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ShengBasCrewServiceImpl extends ServiceImpl<ShengBasCrewMapper, ShengBasCrew> implements ShengBasCrewService {

    @Autowired
    CzBasCrewService czBasCrewService;
    @DataSource(DataSourceEnum.DB3)
    @Override
    public boolean save(ShengBasCrew entity) {
        return super.save(entity);
    }
    @DataSource(DataSourceEnum.DB3)
    @Override
    public boolean saveOrUpdate(ShengBasCrew entity) {
        return super.saveOrUpdate(entity);
    }

    @Override
    public void toSend() {
        QueryWrapper<CzBasCrew> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("code", 130981011).or().likeRight("code", 130903007);
        List<CzBasCrew> czlist = czBasCrewService.list(queryWrapper);
        if (czlist != null){
            for (CzBasCrew czBasCrew:czlist){
                ShengBasCrew shengBasCrew = new ShengBasCrew();
                shengBasCrew.setADDTIME(czBasCrew.getCreateTime());
                shengBasCrew.setAREAACTUAL(Double.parseDouble(czBasCrew.getAreaActual()==null?"0.0":czBasCrew.getAreaActual()));
                shengBasCrew.setCREWID(czBasCrew.getCodeProvince());
                shengBasCrew.setCREWNAME(czBasCrew.getCrewName());
                shengBasCrew.setHEATINGMODE(StringUtils.isEmpty(czBasCrew.getHeatingMode())?null:(czBasCrew.getHeatingMode().equals("普暖")?0:1));
                if (!StringUtils.isEmpty(czBasCrew.getCodeProvince())){
                    shengBasCrew.setHEATSTATIONID(czBasCrew.getCodeProvince().substring(0,13));
                }
                shengBasCrew.setISUSE(czBasCrew.getIsUse());
                shengBasCrew.setUSEDFOR(czBasCrew.getUsedFor());
                this.save(shengBasCrew);
            }
        }
    }
}
